<template>
  <div>
    <el-dialog
      :title="form.id ? '菜单编辑' : '菜单添加'"
      :visible.sync="buffer.Bufferfrom"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree
            :data="menus"
            ref="tree"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            @check="changes"
            :default-checked-keys="bianji"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="角色状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="buffer.Bufferfrom = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["buffer", "mylist", "bianji"],
  name: "",
  components: {},
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: "",
      },
      menus: [],
      dialogTableVisible: false,
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  watch: {
    buffer: {
      handler(newV, oldV) {
        if (newV.Bufferfrom == false) {
          this.form = {
            rolename: "",
            status: "",
            menus: "",
          };
          this.$refs.tree.setCheckedKeys([]);
        }
      },
      deep: true,
    },
  },
  created() {},
  mounted() {
    this.getlist();
  },
  methods: {
    getlist() {
      this.$api.reqGetCate({ istree: true }).then((res) => {
        console.log(res);
        this.menus = res.data.list;
        console.log(this.menus);
      });
    },
    submit() {
      (this.form.id
        ? this.$api.rolEeedit(this.form)
        : this.$api.roleAdd(this.form)
      )
        .then((res) => {
          console.log(res);
          this.buffer.Bufferfrom = false;
          this.$message({ type: "success", message: "操作成功！" });
          this.$emit("channel", "更新数据吧！");
        })
        .catch((err) => console.log(err));
    },
    changes(e, m) {
      console.log(e);
      console.log(m);
      this.form.menus = m.checkedKeys.join(",");
    },
  },
};
</script>
<style scoped lang="scss"></style>
